package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.paramSetting;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysDic;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysDicService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ParamBu {
    @Autowired
    private ISysDicService iSysDicService;

    /**
     * 查询所有系统参数
     */
public SzResult getSysDic() {
    QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("DEL_FLAG", "0");
    queryWrapper.groupBy("DIC_TYPE");
    List<SysDic> list = iSysDicService.list(queryWrapper);
    //new一个list，存放dicType
    List<String> list1 = new ArrayList<>();
    for (SysDic sysDic : list) {
        list1.add(sysDic.getDicType());
    }
    return SzResult.success(list1);
}

    /**
     * 是否开启一级审批
     *
     * @param financeApproveStatus 0：关闭，1：开启
     */
    public SzResult financeApprove(String financeApproveStatus) {
        /*        QueryWrapper<SysDic> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("DEL_FLAG", "0");
        List<SysDic> list = iSysDicService.list(queryWrapper1);
        //键为dicType，值为list的map
        Map<String ,List<SysDic>> map = list.stream().collect(Collectors.groupingBy(SysDic::getDicType));
        //获取financeApprove的list
        List<SysDic> sysDics = map.get("financeApprove");*/
        QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DEL_FLAG", "0");
        queryWrapper.eq("DIC_KEY", "financeApprove");
        SysDic one = iSysDicService.getOne(queryWrapper);
        if (financeApproveStatus.equals("0")) {
            if (one == null) {
                return SzResult.error("没有该系统参数或该系统参数已被删除，请检查");
            }
            one.setDicVal("0");    //关闭一级审批
        } else if (financeApproveStatus.equals("1")) {

            if (one == null) {
                return SzResult.error("没有该系统参数或该系统参数已被删除，请检查");
            }
            one.setDicVal("1");    //开启一级审批
        }
        iSysDicService.updateById(one);
        return SzResult.Success();
    }

    /**
     * 是否开启二级审批
     *
     * @param financeApproveStatus 0：关闭，1：开启
     */
    public SzResult financeApprove2(String financeApproveStatus) {
        //一级审批开启时，二级审批才能开启
        QueryWrapper<SysDic> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("DEL_FLAG", "0");
        queryWrapper1.eq("DIC_KEY", "financeApprove");
        SysDic one = iSysDicService.getOne(queryWrapper1);
        if (one == null) {
            return SzResult.error("没有该系统参数或该系统参数已被删除，请检查");
        }
        if (one.getDicVal().equals("0")) {
            return SzResult.error("一级审批未开启，二级审批不能开启");
        }
        //是否开启二级审批
        QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DEL_FLAG", "0");
        queryWrapper.eq("DIC_KEY", "financeApprove2");
        SysDic one2 = iSysDicService.getOne(queryWrapper);
        if (financeApproveStatus.equals("0")) {
            if (one2 == null) {
                return SzResult.error("没有该系统参数或该系统参数已被删除，请检查");
            }
            one2.setDicVal("0");    //关闭二级审批
        } else if (financeApproveStatus.equals("1")) {
            if (one2 == null) {
                return SzResult.error("没有该系统参数或该系统参数已被删除，请检查");
            }
            one2.setDicVal("1");    //开启二级审批
        }
        iSysDicService.updateById(one2);
        return SzResult.Success();
    }
}
